import { parseToObject } from '@/utils';
import { INITIAL_FORM_QUERY_VALUE, STATUS_TYPES } from '../../constants';
import { message } from '@/utils/util';
import { ORDER_BY_FIELD_LIST } from '@/constants';

export default {
  name: 'header-form',
  props: {
    // 顶级渠道列表
    topChannelList: {
      type: Array,
      default: () => [],
    },
    // 表单默认值
    defaultFormQueryValueStr: String,
    // 查询状态
    querying: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      labelWidth: '112px',

      statusTypes: STATUS_TYPES,
      orderByFiledList: ORDER_BY_FIELD_LIST,

      formQuery: { ...INITIAL_FORM_QUERY_VALUE },
      channelRangeStart: undefined, // 建议原价毛利率最小值
      channelRangeEnd: undefined, // 建议原价毛利率最大值
      activityRangeStart: undefined, // 建议特价毛利率最小值
      activityRangeEnd: undefined, // 建议特价毛利率最大值
    };
  },
  watch: {
    // 监听传入的表单默认值变化--特殊场景如跳到当前页面携带查询参条件
    defaultFormQueryValueStr(newValue) {
      if (!newValue) {
        return;
      }
      this.formQuery = {
        ...this.formQuery,
        ...parseToObject(newValue),
      };
    },
  },
  methods: {
    onQuery() {
      if (this.channelRangeStart !== undefined && this.channelRangeEnd !== undefined && this.channelRangeStart > this.channelRangeEnd) {
        message('提示', '查询条件【建议原价毛利率】最小值不得大于最大值', 'warning');
        return;
      }
      if (this.activityRangeStart !== undefined && this.activityRangeEnd !== undefined && this.activityRangeStart > this.activityRangeEnd) {
        message('提示', '查询条件【建议特价毛利率】最小值不得大于最大值', 'warning');
        return;
      }
      if ((this.formQuery.orderByFiled && !this.formQuery.orderBy) || (!this.formQuery.orderByFiled && this.formQuery.orderBy)) {
        message('提示', '查询条件【筛选数据】不完整', 'warning');
        return;
      }

      this.formQuery = {
        ...this.formQuery,
        newChannelPriceProfRange: {
          start: this.channelRangeStart,
          end: this.channelRangeEnd,
        },
        newActivityPriceProfRange: {
          start: this.activityRangeStart,
          end: this.activityRangeEnd,
        },
      };
      this.$emit('onQuery', { ...this.formQuery });
    },
    reset() {
      this.formQuery = { ...INITIAL_FORM_QUERY_VALUE };
      this.channelRangeStart = undefined;
      this.channelRangeEnd = undefined;
      this.activityRangeStart = undefined;
      this.activityRangeEnd = undefined;
      this.$emit('onQuery', { ...this.formQuery });
    },
  },
  async mounted() {},
};
